/**
 * Copyright [2020] [LiBo/Alex of copyright liboware@gmail.com ]
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.callcenter.execute.schema;

import cn.hutool.core.collection.CollectionUtil;
import com.hyts.callcenter.base.CallCenterRequest;
import com.hyts.callcenter.base.CallCenterResponse;
import com.hyts.callcenter.error.CallTaskException;
import com.hyts.callcenter.execute.CallTaskExecute;
import com.hyts.callcenter.extense.event.EventListenerRegistry;
import com.hyts.callcenter.model.bo.TaskModel;
import com.hyts.callcenter.model.event.TaskRemoveEvent;
import com.hyts.callcenter.model.event.TaskStartEvent;
import com.hyts.callcenter.model.po.OutboundLinePersistModel;
import com.hyts.callcenter.model.po.TaskPersistModel;
import com.hyts.callcenter.schema.CallTaskSchemaApi;
import lombok.extern.slf4j.Slf4j;
import net.sf.cglib.beans.BeanCopier;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Objects;

/**
 * @project-name:callcenter
 * @package-name:com.hyts.callcenter.engine
 * @author:LiBo/Alex
 * @create-date:2021-11-06 10:41 下午
 * @copyright:libo-alex4java
 * @email:liboware@gmail.com
 * @description: 默认的呼叫任务操作处理实现类
 */
@Slf4j
@DubboService(version = "1.0.0")
public class DefaultCallTaskSchemaApi implements CallTaskSchemaApi {


    @Autowired
    ObjectProvider<EventListenerRegistry> eventListenerRegistry;


    @Autowired
    CallTaskExecute callTaskExecute;


    /**
     * 存储任务数据模型机制
     * @param taskModelCallCenterRequest
     * @return
     */
    @Override
    public CallCenterResponse saveTask(CallCenterRequest<TaskModel> taskModelCallCenterRequest) {
        log.info("开始存储呼叫任务模型数据信息：{}",taskModelCallCenterRequest.getEnity());
        TaskPersistModel persistModel = new TaskPersistModel();
        BeanCopier copier = BeanCopier.create(TaskModel.class,TaskPersistModel.class,false);
        copier.copy(taskModelCallCenterRequest.getEnity(),persistModel,null);
        List<OutboundLinePersistModel> outboundLines = persistModel.getOutboundLines();
        return null;
    }


    /**
     * 启动任务
     * @param callCenterRequest
     * @return
     */
    @Override
    public CallCenterResponse startTask(CallCenterRequest<Long> callCenterRequest) {
        CallCenterResponse<TaskModel> callCenterResponse = null;
        try {
            callCenterResponse = callTaskExecute.start(new TaskModel(callCenterRequest.getEnity()));
            log.info("发起的事件监听注册服务组件：{}",eventListenerRegistry.getIfAvailable());
//            eventListenerRegistry.getIfAvailable().
//                    publish(new TaskStartEvent(callCenterResponse.getEntity().getTaskId()));
            if(callCenterResponse.isSuccess()){
                eventListenerRegistry.getIfAvailable().publish(new TaskStartEvent(callCenterResponse.getEntity().getTaskId()));
            }
            log.info("完成时间监听注册服务机制组件！");
        } catch (Exception e) {
            log.error("执行相关的事件监听失败！",e);
        }
        return callCenterResponse;
    }

    /**
     * 启动任务集合
     * @param callCenterRequest
     * @return
     */
    @Override
    public CallCenterResponse startTasks(CallCenterRequest<List<Long>> callCenterRequest) {
        //校验数据参数
        try {
            if(Objects.nonNull(callCenterRequest) && CollectionUtil.isNotEmpty(callCenterRequest.getEnity())){
                callCenterRequest.getEnity().stream().map(CallCenterRequest::new).forEach(this::startTask);
            }
            return CallCenterResponse.success("start task is success!");
        } catch (Exception e) {
            log.error("start tasks is failure!",e);
            return CallCenterResponse.failure("start task is failure!");
        }
    }

    /**
     * 删除任务信息数据
     * @param taskModelCallCenterRequest
     * @return
     */
    @Override
    public CallCenterResponse removeTask(CallCenterRequest<Long> taskModelCallCenterRequest) {
        if(Objects.isNull(taskModelCallCenterRequest.getEnity())){
            throw new CallTaskException("the task id is not null!");
        }
        CallCenterResponse callCenterResponse = callTaskExecute.remove(new TaskModel(taskModelCallCenterRequest.getEnity()));
        if(callCenterResponse.isSuccess()){
            TaskRemoveEvent taskRemoveEvent = new TaskRemoveEvent(taskModelCallCenterRequest.getEnity());
            eventListenerRegistry.getIfAvailable().publish(taskRemoveEvent);
        }
        return null;
    }

    @Override
    public CallCenterResponse checkTaskAiLimited(CallCenterRequest<Long> taskModelCallCenterRequest) {
        return null;
    }

    @Override
    public CallCenterResponse getTenantHavingAiNum(CallCenterRequest<Long> tenantId) {
        return null;
    }


    @Override
    public CallCenterResponse stopTask(CallCenterRequest<Long> callCenterRequest) {
        return null;
    }
}
